package cn.tiga.pet.service.impl;

import cn.tiga.basic.exception.BusinessExeption;
import cn.pping.basic.util.*;
import cn.pping.order.domain.OrderPetAcquisition;
import cn.pping.order.mapper.OrderPetAcquisitionMapper;
import cn.tiga.basic.util.CodeGenerateUtils;
import cn.tiga.basic.util.DistanceUtils;
import cn.tiga.basic.util.PageList;
import cn.tiga.basic.util.Point;
import cn.tiga.org.domain.Employee;
import cn.tiga.org.domain.Shop;
import cn.tiga.org.mapper.EmployeeMapper;
import cn.tiga.org.mapper.ShopMapper;
import cn.tiga.org.service.IBaiduAiAuditService;
import cn.tiga.pet.domain.Pet;
import cn.tiga.pet.domain.SearchMasterMsg;
import cn.tiga.pet.domain.SearchMasterMsgAuditLog;
import cn.tiga.pet.dto.AcceptDto;
import cn.tiga.pet.mapper.PetMapper;
import cn.tiga.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.tiga.pet.mapper.SearchMasterMsgMapper;
import cn.tiga.pet.query.SearchMasterMsgQuery;
import cn.tiga.pet.service.ISearchMasterMsgService;
import cn.tiga.basic.service.impl.BaseServiceImpl;
import cn.tiga.user.domain.User;
import cn.tiga.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pping
 * @since 2022-08-09
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Override
    public void publish(SearchMasterMsg ssm, Long id) {
        // 先保存user id
        User user = userMapper.loadBylogininfoId(id);
        // 设置userid
        ssm.setUser_id(user.getId());
        // 获取店铺地址找到节点
        Point point = DistanceUtils.getPoint(ssm.getAddress());
        // 找到最近店铺
        Shop nearestShop = DistanceUtils.getNearestShop(point, shopMapper.loadAll());
        // 保存最近店铺信息
        ssm.setShop_id(nearestShop.getId());
        // 保存用户信息
        searchMasterMsgMapper.add(ssm);

        // 百度Ai审核
        Boolean aBoolean = baiduAiAuditService.textAudit(ssm.getTitle() + ssm.getName() + ssm.getAddress() + ssm.getCoatColor());
        Boolean imgBoolean = baiduAiAuditService.imageAudit(ssm.getResources());

        if (aBoolean && imgBoolean) {
            // 审核通过修改状态
            ssm.setState(1);
            ssm.setNote("待处理");
            searchMasterMsgMapper.update(ssm);
            // 2. 添加审核日志
            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(ssm.getId());
            smmal.setState(1);
            smmal.setAudit_time(new Date());
            smmal.setNote("待处理状态");
            // 保存审核日志信息
            searchMasterMsgAuditLogMapper.add(smmal);
        } else {
            // 1. 修改状态     note
            ssm.setNote("信息驳回,审核未通过");
            searchMasterMsgMapper.update(ssm);
            // 添加审核日志
            SearchMasterMsgAuditLog smmal = new SearchMasterMsgAuditLog();
            smmal.setMsg_id(ssm.getId());
            smmal.setState(0);
            smmal.setAudit_time(new Date());
            smmal.setNote("信息异常,已驳回");
            searchMasterMsgAuditLogMapper.add(smmal);
            throw new BusinessExeption("输入信息存在异常,请重新查阅信息");
        }

    }

    /**
     * @Title: toHandle 待处理状态
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/8/10 14:04
     * @Parameters: [query, id]
     * @Return cn.pping.basic.util.PageList<cn.pping.pet.domain.SearchMasterMsg>
     */

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, Long id) {
        // 设置状态为待处理状态
        query.setState(1);
        // 根据登录信息获取员工信息
        Employee employee = employeeMapper.loadByLogininfoId(id);
        // 判断店铺是否为空 员工 没有shop的就是平台管理员工 有id就是店铺管理员就找到自己的店铺
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    /*
     * @Title: 如果是用户
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date:  2022/8/10 14:05
     * @Parameters:
     * @Return
     */

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Long id) {
        // 查询到用户id
        User user = userMapper.loadBylogininfoId(id);
        query.setUserId(user.getId());  // <if userId！=null> and user_id=#{userId}<>

        return super.pageQuery(query);
    }

    /**
     * @param id
     */
    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }


    @Override
    public PageList<SearchMasterMsg> toAudit(SearchMasterMsgQuery query, Long id) {
        query.setState(0);
        Employee employee = employeeMapper.loadByLogininfoId(id);
        // 判断店铺是否为空 员工 没有shop的就是平台管理员工 有id就是店铺管理员就找到自己的店铺
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    /**
    * @Title: accept接单
    * @Description: TODO
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/8/11 1:47
    * @Parameters: [dto, id]
    * @Return void
    */

    @Override
    public void accept(AcceptDto dto, Long id) {
        Long empId = dto.getHandler();
        Long msgId = dto.getMsgId();
        String note = dto.getNote();


        // 根据smmid查询订单，如果查的到，该订单正在处理中，
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadBySmmId(msgId);
        if(order!=null){ // 之前接单了
            throw  new BusinessExeption("订单正在处理中。。。！");
        }
        // 根据寻主生成一个pet信息 应为是平台收取回来
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(msgId);
        // 根据寻主信息生成pet信息
        Pet pet = searchMasterMsg2Pet(searchMasterMsg);
        // 保存,保存id
        petMapper.add(pet);

        // 根据dto,pet,searchMasterMsg生成收购订单
        OrderPetAcquisition opa = ininOrderPetAcquisition(dto,pet,searchMasterMsg);
        // 3. 保存订单
        orderPetAcquisitionMapper.add(opa);


    }

    private OrderPetAcquisition ininOrderPetAcquisition(AcceptDto dto, Pet pet, SearchMasterMsg searchMasterMsg) {

        // 创建一个订单对象，赋值
        OrderPetAcquisition order = new OrderPetAcquisition();

        // 随机生成一个订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMsg.getUser_id()));
        // 摘要
        order.setDigest("【"+searchMasterMsg.getTitle()+"】");
        // 最后确认时间 定时任务
        order.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));

        order.setState(0);// 待处理
        order.setPrice(searchMasterMsg.getPrice());
        order.setPet_id(pet.getId());
        order.setUser_id(searchMasterMsg.getUser_id());
        order.setShop_id(searchMasterMsg.getShop_id());
        order.setAddress(searchMasterMsg.getAddress());
        order.setEmp_id(dto.getHandler());
        order.setSearch_master_msg_id(dto.getMsgId());
        return order;
    }

    private Pet searchMasterMsg2Pet(SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(searchMasterMsg.getPrice());
        pet.setType_id(searchMasterMsg.getPetType());
        pet.setResources(searchMasterMsg.getResources());
        pet.setShop_id(searchMasterMsg.getShop_id());
        pet.setSearch_master_msg_id(searchMasterMsg.getId());
        return pet;
    }

}
